<template>
  <div class="hardwareOrder">
    <!-- 硬件订单 -->
    <!-- 短信订单页面 -->
    <div class="search">
      <el-input
        size="mini"
        placeholder="输入订单号/客户名称/客户电话"
        clearable
        style="width: 250px"
        @keyup.enter.native="handleSearch"
      ></el-input>
      <el-button
        size="mini"
        type="primary"
        @click="handleSearch"
        style="padding: 8px 20px;margin-left: 10px;border: none;color: #626366;background-color: #f1f1f1"
      >搜索</el-button>
    </div>
    <!-- 筛选部分 -->
    <div class="filtrate">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="订单状态">
          <el-radio-group v-model="form.sv_delivery_status" size="mini">
            <el-radio-button label="-1">全部</el-radio-button>
            <el-radio-button label="0">待付款</el-radio-button>
            <el-radio-button label="1">待发货</el-radio-button>
            <el-radio-button label="2">已发货</el-radio-button>
            <el-radio-button label="3">已收货</el-radio-button>
            <el-radio-button label="4">退货</el-radio-button>
            <el-radio-button label="5">交易完成</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="付款时间">
          <el-row>
            <el-col :lg="24" :xl="6">
              <el-radio-group v-model="form.sv_finish_time" size="mini" @change="hangleChangeTime1">
                <el-radio-button label="1">全部</el-radio-button>
                <el-radio-button label="2">7天内</el-radio-button>
                <el-radio-button label="3">15天内</el-radio-button>
                <el-radio-button label="4">一个月内</el-radio-button>
              </el-radio-group>
            </el-col>
            <el-col :span="6">
              <div class="block">
                <el-date-picker
                  v-model="form.sv_finish_time"
                  type="datetimerange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  size="mini"
                  @change="handleChangeFinish"
                ></el-date-picker>
              </div>
            </el-col>
            <el-col :span="2" :offset="10">
              <el-button type="primary" size="mini" @click="handleFiltrate">确定</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </div>
    <!-- 订单统计 -->
    <Statistics :totalMoney="totalMoney" :orderLength="orderLength"></Statistics>
    <!-- 表格部分 -->
    <div class="tableData">
      <el-table style="width: 100%;margin-bottom: 40px" :data="tableData">
        <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
        <el-table-column align="center" min-width="90" label="订单号">
          <template slot-scope="scope">
            <span>{{scope.row.sv_order_number.substring(scope.row.sv_order_number.length - 8)}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="90" label="店铺id" prop="user_id"></el-table-column>
        <el-table-column align="center" min-width="100" label="客户名称" prop="sv_contact_name"></el-table-column>
        <el-table-column align="center" min-width="120" label="客户电话" prop="sv_contact_phone">
          <template slot-scope="scope">
            <span
              class="viewDetails"
              @click="handleWatchDetail(scope.$index, scope.row)"
            >{{scope.row.sv_contact_phone}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="160" label="订单信息" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.productList" :key="index">
              {{item.product_name}}——
              [价格:{{item.sv_product_unitprice}}][数量:{{item.sv_product_num}}]
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="代币(元)" prop="sv_price_total"></el-table-column>
        <el-table-column align="center" min-width="160" label="下单时间" prop="sv_delivery_time"></el-table-column>
        <el-table-column align="center" min-width="160" label="付款时间" prop="sv_finish_time"></el-table-column>
        <el-table-column align="center" label="支付方式" prop="order_payment"></el-table-column>
        <el-table-column align="center" min-width="100" label="订单状态">
          <template slot-scope="scope">
            <span
              :style="{color: scope.row.sv_delivery_status == 0 || scope.row.sv_delivery_status == 4 ? 'red' : ''}"
            >{{orderStatus[scope.row.sv_delivery_status].label}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <span size="mini" class="viewDetails" @click="handleShipments(scope.row)">发货</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页器部分 -->
    <Pagination
      :total="total"
      :pageIndex="pageIndex"
      :pageSize="pageSize"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    ></Pagination>
    <!-- 弹出框 -->
    <div class="orderDetail" v-show="visible">
      <div class="content" style="width: 500px">
        <h3 class="title" style="font-size: 16px;margin-bottom: 20px">订单详情</h3>
        <div class="close" @click="visible = false">
          <i class="fa fa-times" aria-hidden="true"></i>
        </div>
        <div class="aboutOrder">
          <div class="aboutItem order_num">订单号：{{formLabelAlign.sv_order_number}}</div>
          <div class="aboutItem order_price">代币(元)：{{formLabelAlign.sv_price_total}}</div>
          <div class="aboutItem order_paidTime">付款时间：{{formLabelAlign.sv_finish_time}}</div>
          <div
            class="aboutItem order_status"
          >订单状态：{{orderStatus[formLabelAlign.sv_delivery_status].label}}</div>
        </div>
        <div class="customerInfo">
          <div class="customerItem customer-name">客户名称：{{formLabelAlign.sv_contact_name}}</div>
          <div class="customerItem customer-phone">客户电话：{{formLabelAlign.sv_contact_phone}}</div>
          <div class="customerItem customer-adress">收货地址：{{formLabelAlign.sv_contact_address}}</div>
        </div>
        <div class="express">快递单号：{{formLabelAlign.shipmentsnumber}}</div>
        <div class="detailTable">
          <el-table :data="formLabelAlign.productList" border style="width: 100%" size="mini">
            <el-table-column align="center" prop="product_name" label="商品名称"></el-table-column>
            <el-table-column align="center" prop="sv_product_unitprice" label="单价(元)"></el-table-column>
            <el-table-column align="center" prop="sv_product_num" label="数量"></el-table-column>
            <el-table-column align="center" prop="sv_product_total" label="合计代币(元)"></el-table-column>
          </el-table>
        </div>
        <div class="detailBtnBox">
          <el-button
            type="primary"
            style="padding: 8px 20px"
            size="mini"
            @click="visible = false"
          >确认</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Statistics from "@/components/Statistics";
import Pagination from "@/components/Pagination";
export default {
  data() {
    return {
      // 表单数据
      form: {
        sv_delivery_status: -1,
        sv_finish_time: "",
        sv_finish_time1: "",
        sv_finish_time2: ""
      },
      search: "",
      // 表格数据
      tableData: [],
      // 订单状态
      orderStatus: [
        { id: 0, label: "待付款" },
        { id: 1, label: "待发货" },
        { id: 2, label: "已发货" },
        { id: 3, label: "已收货" },
        { id: 4, label: "退货" },
        { id: 5, label: "交易完成" }
      ],
      // 统计总代币
      totalMoney: null,
      // 分页器的数据
      total: 0, // 总条数
      pageIndex: 1, // 当前页
      pageSize: 10, // 每页数量
      // 每页订单数量
      orderLength: "",
      // 这个是弹出框里的数据
      formLabelAlign: { sv_delivery_status: 0 },
      visible: false,
      // 令牌
      myToken: ""
    };
  },
  components: {
    Pagination,
    Statistics
  },
  methods: {
    // 初始化函数
    init() {
      // 获取令牌
      this.myToken = sessionStorage.getItem("myToken");
      // 初始化列表
      this.getOrderData();
      // 格式化时间
      this.timeFormatting = this.utils.timeFormatting();
    },
    // 获取数据
    getOrderData() {
      // this.$axios({
      //   url: `/UserInfo/PTHardwareGetOrderList?pageIndex=${this.pageIndex}&pageSize=${this.pageSize}&sv_delivery_status=${this.form.sv_delivery_status}&sv_finish_time1=${this.form.sv_finish_time1}&sv_finish_time2=${this.form.sv_finish_time2}&search=${this.search}&key=${this.myToken}`
      // }).then(res => {
      //   this.totalMoney = null;
      //   const { dataList, total } = res.data.values;
      //   this.total = total;
      //   if (dataList == null) {
      //     this.tableData = dataList;
      //     return;
      //   }
      //   this.orderLength = dataList.length;
      //   this.formLabelAlign = dataList[0];
      //   this.tableData = dataList.map(v => {
      //     // 计算当前合计代币
      //     this.totalMoney += v.sv_price_total;
      //     // 格式化时间
      //     v.sv_finish_time = new Date(v.sv_finish_time).Format(
      //       "yyyy-MM-dd hh:mm:ss"
      //     );
      //     v.sv_delivery_time = new Date(v.sv_delivery_time).Format(
      //       "yyyy-MM-dd hh:mm:ss"
      //     );
      //     return { ...v };
      //   });
      // });
    },
    // 搜索
    handleSearch() {
      this.pageIndex = 1;
      this.getOrderData();
    },
    // 注册时间筛选
    hangleChangeTime1(val) {
      this.form.sv_finish_time2 = "";
      if (val == 1) {
        this.form.sv_finish_time1 = "";
      } else if (val == 2) {
        this.form.sv_finish_time1 = new Date(
          new Date().getTime() - 86400000 * 7
        ).Format("yyyy-MM-dd hh:mm:ss");
      } else if (val == 3) {
        this.form.sv_finish_time1 = new Date(
          new Date().getTime() - 86400000 * 15
        ).Format("yyyy-MM-dd hh:mm:ss");
      } else if (val == 4) {
        this.form.sv_finish_time1 = new Date(
          new Date().getTime() - 86400000 * 30
        ).Format("yyyy-MM-dd hh:mm:ss");
      }
    },
    // 格式化付款时间
    handleChangeFinish(val) {
      // console.log(val);
      if (val != null) {
        this.form.sv_finish_time1 = new Date(val[0]).Format(
          "yyyy-MM-dd hh:mm:ss"
        );
        this.form.sv_finish_time2 = new Date(val[1]).Format(
          "yyyy-MM-dd hh:mm:ss"
        );
      } else {
        this.form.sv_finish_time1 = "";
        this.form.sv_finish_time2 = "";
      }
    },
    handleFiltrate() {
      this.pageIndex = 1;
      this.getOrderData();
    },
    // 查看详情
    handleWatchDetail(index, row) {
      this.visible = true;
      this.formLabelAlign = row;
    },
    // 发货
    handleShipments(val) {
      this.$axios({
        url: `/UserInfo/PTHardwareShipments?sv_order_number=${val.sv_order_number}&shipmentsnumber=${val.shipmentsnumber}&key=${this.myToken}`
      }).then(res => {
        if (res.data.values > 0) {
          this.$message({
            type: "success",
            message: "发货成功"
          });
        } else {
          this.$message.error("操作失败");
        }
      });
    },
    // 分页器的方法
    handleSizeChange(val) {
      this.pageSize = val;
      this.getOrderData();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getOrderData();
    }
  },
  mounted() {
    // 初始化函数
    this.init();
  }
};
</script>

<style lang="less" scoped>
@deep: ~">>>";
.hardwareOrder {
  background-color: #fff;
  padding: 20px;
  .search {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 20px;
  }
  .tableData {
    .viewDetails {
      color: #3399ff;
      cursor: pointer;
    }
    .flex {
      display: flex;
      justify-content: space-around;
      .el-select {
        width: 100px;
      }
    }
  }
  // 更改表头颜色
  .tableData @{deep} .el-table th {
    background-color: #fafafa;
    color: #252525;
    font-weight: 100;
  }
  .el-table::before {
    height: 0;
  }
  // 弹出框
  .orderDetail {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-color: rgba(0, 0, 0, 0.3);
    .content {
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      padding: 20px;
      box-sizing: border-box;
      font-size: 14px;
      border-radius: 10px;
      .aboutOrder {
        display: flex;
        flex-wrap: wrap;
        margin-bottom: 15px;
        .aboutItem {
          margin: 5px 0;
          &:nth-of-type(2n-1) {
            width: 70%;
          }
          &:nth-of-type(2n) {
            width: 30%;
          }
        }
      }
      .customerInfo {
        margin-bottom: 15px;
        .customerItem {
          margin: 5px 0;
        }
      }
      .express {
        margin-bottom: 20px;
        .expressNum {
          padding: 1px 5px;
          border: 1px solid #ccc;
        }
      }
      .detailTable {
        margin-bottom: 20px;
      }
      .detailBtnBox {
        text-align: center;
        .detailBtn {
          background-color: #00cc99;
          color: #fff;
          padding: 7px 30px;
          border-radius: 8px;
        }
      }
      .close {
        position: absolute;
        top: 10px;
        right: 10px;
        cursor: pointer;
        color: #666;
        font-size: 20px;
      }
    }
  }
  .filtrate {
    padding: 5px 10px;
    background-color: #f1f1f1;
    position: relative;
    .el-radio-button {
      margin-right: 10px;
    }
  }
  // 筛选的每一项
  .filtrate @{deep} .el-form-item {
    margin-bottom: 0;
  }
  // 筛选项的标题
  .filtrate @{deep} .el-form-item__label {
    color: #000;
    font-weight: 600;
  }
  // 筛选项的内容
  .filtrate @{deep} .el-radio-button__inner {
    border: 1px solid #f1f1f1;
    background-color: #f1f1f1;
    font-size: 14px;
    color: #626366;
  }
  // 筛选项选中内容
  .filtrate @{deep} .is-active .el-radio-button__inner {
    background-color: #f1f1f1;
    border: 1px solid #439fff;
    box-shadow: unset;
    color: #666;
    border-radius: 4px;
  }
}
</style>
